﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Compiler
{
    /// <summary>
    /// 文法产生式
    /// </summary>
    public static class Production
    {
        /// <summary>
        /// 产生式
        /// </summary>
        public static List<KeyValuePair<string, string>> Productions = new List<KeyValuePair<string, string>>();
        /// <summary>
        /// 生成中间代码用的产生式
        /// </summary>
        public static List<KeyValuePair<string, string>> TransProductions = new List<KeyValuePair<string, string>>();

        /// <summary>
        /// 读取产生式
        /// </summary>
        public static void LoadProductions()
        {
            try
            {
                using (StreamReader reader = new StreamReader("../../Files/productions.txt"))
                {
                    lock (Productions)
                    {
                        for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
                        {

                            var items = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            var left = items[0];
                            var rights = string.Join(" ", items.Skip(2));
                            Productions.Add(new KeyValuePair<string, string>(left, rights));
                        }
                    }
                }
                using (StreamReader reader = new StreamReader("../../Files/transProductions.txt"))
                {
                    lock (TransProductions)
                    {
                        for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
                        {

                            var items = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            var left = items[0];
                            var rights = string.Join(" ", items.Skip(2));
                            TransProductions.Add(new KeyValuePair<string, string>(left, rights));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Compiler.Log(e);
                return;
            }
        }
    }
}
